//
//  KRSearchGetListAlbumCell.m
//  cookBook
//
//  Created by kuroken on 16/1/26.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRSearchGetListAlbumCell.h"
#import <UIImageView+WebCache.h>
#import <PureLayout.h>
#import "KRConst.h"
#import "KRSearchGetResultAlbum.h"

// cell重用标识
static NSString * const KRSearchGetListAlbum = @"searchGetListAlbum";

@interface KRSearchGetListAlbumCell ()

/** 封面 iamgeView */
@property (nonatomic, weak) UIImageView *coverImageView;
/** 专辑标签 label */
@property (nonatomic, weak) UILabel *tagView;
/** xx道菜 label */
@property (nonatomic, weak) UILabel *countLabel;
/** 标题 label */
@property (nonatomic, weak) UILabel *titleLabelView;
/** xx收藏 xx浏览 label */
@property (nonatomic, weak) UILabel *collectionLabel;
/** 简介 label */
@property (nonatomic, weak) UILabel *introLabel;
@end

@implementation KRSearchGetListAlbumCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRSearchGetListAlbumCell *cell = [tableView dequeueReusableCellWithIdentifier:KRSearchGetListAlbum];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KRSearchGetListAlbum];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
//        self.selectionStyle = UITableViewCellSelectionStyleGray;
        
        [self setupSubview];
        [self addConstraint];
    }
    return self;
}

- (void)setAlbum:(KRSearchGetResultAlbum *)album
{
    _album = album;
    
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:album.cover] placeholderImage:[UIImage imageNamed:@"default_r172-118"]];
    [self.countLabel setText:[NSString stringWithFormat:@" %@道菜", album.recipeCount]];
    [self.titleLabelView setText:album.title];
    [self.collectionLabel setText:album.collection];
    [self.introLabel setText:album.intro];
}

/**
 *  初始化子控件
 */
- (void)setupSubview
{
    /** 图片 iamgeView */
    UIImageView *coverImageView = [[UIImageView alloc] init];
    coverImageView.contentMode = UIViewContentModeScaleAspectFill;
    coverImageView.clipsToBounds = YES;
    [self.contentView addSubview:coverImageView];
    self.coverImageView = coverImageView;
    
    /** 专辑标签 imageView */
    UILabel *tagView = [[UILabel alloc] init];
    [tagView setText:@" 专辑 "];
    [tagView setFont:[UIFont systemFontOfSize:13]];
    [tagView setTextColor:[UIColor whiteColor]];
#warning 解决cell被选中后label背景色被清除的问题
    [tagView setBackgroundColor:[UIColor clearColor]];
    [tagView.layer setBackgroundColor:KRColor(111, 143, 18).CGColor];
    [self.coverImageView addSubview:tagView];
    self.tagView = tagView;
    
    /** xx道菜 label */
    UILabel *countLabel = [[UILabel alloc] init];
    [countLabel setTextColor:[UIColor whiteColor]];
    [countLabel setFont:[UIFont systemFontOfSize:13]];
    [countLabel setBackgroundColor:[UIColor clearColor]];
    [countLabel.layer setBackgroundColor:[KRColorWithAlpha(100, 100, 100, 0.2) CGColor]];
    [self.coverImageView addSubview:countLabel];
    self.countLabel = countLabel;
    
    /** 标题 label */
    UILabel *titleLabelView = [[UILabel alloc] init];
    [titleLabelView setFont:[UIFont systemFontOfSize:16]];
    [titleLabelView setNumberOfLines:0];
    [self.contentView addSubview:titleLabelView];
    self.titleLabelView = titleLabelView;
    
    /** xx收藏 xx浏览 label */
    UILabel *collectionLabel = [[UILabel alloc] init];
    [collectionLabel setFont:[UIFont systemFontOfSize:13]];
    [collectionLabel setTextColor:[UIColor grayColor]];
    [self.contentView addSubview:collectionLabel];
    self.collectionLabel = collectionLabel;
    
    /** 简介 label */
    UILabel *introLabel = [[UILabel alloc] init];
    [introLabel setNumberOfLines:2];
    [introLabel setFont:[UIFont systemFontOfSize:13]];
    [introLabel setTextColor:[UIColor grayColor]];
    [self.contentView addSubview:introLabel];
    self.introLabel = introLabel;
}

/**
 *  添加约束
 */
- (void)addConstraint
{
    [self.coverImageView autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [self.coverImageView autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:12];
    [self.coverImageView autoSetDimensionsToSize:CGSizeMake(90, 95)];
    
    [self.tagView autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [self.tagView autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    
    [self.countLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:3];
    [self.countLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.countLabel autoPinEdgeToSuperviewEdge:ALEdgeRight];

    [self.titleLabelView autoPinEdge:ALEdgeTop toEdge:ALEdgeTop ofView:self.coverImageView];
    [self.titleLabelView autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.coverImageView withOffset:10];
    [self.titleLabelView autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    
    
    [self.collectionLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.titleLabelView withOffset:7];
    [self.collectionLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.titleLabelView];
    
    [self.introLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.collectionLabel withOffset:7];
    [self.introLabel autoPinEdge:ALEdgeBottom toEdge:ALEdgeBottom ofView:self.coverImageView];
    [self.introLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.titleLabelView];
    [self.introLabel autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
